//
//  MyHeaderStudyAndClassViewController.m
//  ZhongWangYuanApp
//
//  Created by liuyingping on 2019/1/29.
//  Copyright © 2019年 apple. All rights reserved.
//

#import "MyHeaderStudyAndClassViewController.h"
#import "LPZWYChooseClassClassModel.h"
#import "LPZWYCommonSpecilTableViewCell.h"
#import "YBProjectDetailViewController.h"
#define spicalCell @"LPZWYCommonSpecilTableViewCell"
#import "LPZWYCommonClassTableViewCell.h"
#import "ZJVidoViewController.h"
#define classCell @"LPZWYCommonClassTableViewCell"
#import "LPHeaderDetailHeaderVIew.h"

@interface MyHeaderStudyAndClassViewController ()<UITableViewDelegate,UITableViewDataSource,LPHeaderDetailHeaderVIewDelegate>
/**    */
@property (strong, nonatomic) UITableView  *tableView;
@property (nonatomic , strong) NSMutableArray * dataArray;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;
/**  页数  */
@property (assign, nonatomic) NSInteger  page;
/**    */
@property (strong, nonatomic)  LPHeaderDetailHeaderVIew *header;
/**    */
@property (strong, nonatomic) NSString  *timeString;


@end

@implementation MyHeaderStudyAndClassViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.timeString = @"";
    self.titlelabel.text = self.text;
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        
        [weakSelf netWorking_header];

    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        weakSelf.page ++;
        [weakSelf netWorking_footer];
    }];
    [self layoutUI];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView.mj_header beginRefreshing];
}


#pragma mark - 数据
//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    if ([self.text isEqualToString:@"已学课程"]) {
        weakSelf.page = 1;
        [LPRequestManager request_getMyWaitCoursewareWithstudentid:YBUSERINFOMODEL.studentId pageindex:[NSString stringWithFormat:@"%ld",(long)self.page] isfinished:@"1" year:self.timeString successBlock:^(id object) {
            NSString *result = [[object objectForKey:@"data"] objectForKey:@"result"];
            if ([result integerValue] == 1) {
                NSArray *array = [[object objectForKey:@"data"] objectForKey:@"dataList"];
                if (array.count == 0) {
                    [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                    
                }else{
                    weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array] mutableCopy];
                    [weakSelf.tableView reloadData];
                }
                
            }else{
                [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
            }
            [weakSelf.tableView.mj_header endRefreshing];
            
        } failBlock:^(id object) {
            [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
    }else if ([self.text isEqualToString:@"结业专题"]){
        weakSelf.page = 0;
        [LPRequestManager request_getMyClassListUseWeicatAppWithpageindex:self.page * 10 graduatestatus:@"1" year:self.timeString successBlock:^(id object) {
            NSString *result = [[object objectForKey:@"data"] objectForKey:@"result"];
            if ([result integerValue] == 1) {
                NSArray *array = [[object objectForKey:@"data"] objectForKey:@"dataList"];
                if (array.count == 0) {
                    [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                    
                }else{
                    weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array] mutableCopy];
                    [weakSelf.tableView reloadData];
                }
                
            }else{
                [YJProgressHUD showSuccess:[object objectForKey:@"message"] inview:[UIApplication sharedApplication].keyWindow];
            }
            [weakSelf.tableView.mj_header endRefreshing];
            
        } failBlock:^(id object) {
            [YJProgressHUD showSuccess:@"网络错误" inview:[UIApplication sharedApplication].keyWindow];
            [weakSelf.tableView.mj_header endRefreshing];
        }];
    }else{
            weakSelf.page = 1;
            [LPRequestManager request_getmyclassscoreWithyear:self.timeString pageindex:self.page  successBlock:^(id object) {
                NSString *code = [[object objectForKey:@"data"] objectForKey:@"result"];
                
                if ([code integerValue] == 1) {
                    NSArray *array = [[object objectForKey:@"data"] objectForKey:@"dataList"];
                    NSString *score =  [array.firstObject objectForKey:@"score"];
                    if (array.count == 0) {
                        [LYEmptyManager showNoDataWithView:weakSelf.tableView];
                        score = @"0";
                    }else{
                        weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array] mutableCopy];
                        [weakSelf.tableView reloadData];
                    }
                    
                    
                    weakSelf.header.score = score;
                    weakSelf.header.text = weakSelf.text;
                }
                 [weakSelf.tableView.mj_header endRefreshing];
            } failBlock:^(id object) {
                 [weakSelf.tableView.mj_header endRefreshing];
            }];
        
        
    }
   
}

//上拉加载
- (void)netWorking_footer{
    
    __weak typeof(self) weakSelf = self;
    if ([self.text isEqualToString:@"已学课程"]) {
        [LPRequestManager request_getMyWaitCoursewareWithstudentid:YBUSERINFOMODEL.studentId pageindex:[NSString stringWithFormat:@"%ld",(long)self.page] isfinished:@"1" year:self.timeString successBlock:^(id object) {
            NSString *result = [[object objectForKey:@"data"] objectForKey:@"result"];
            if ([result integerValue] == 1) {
                NSArray *array = [[object objectForKey:@"data"] objectForKey:@"dataList"];
                if (array.count == 0) {
                    _page --;
                    
                }else{
                    [weakSelf.dataArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array]] ;
                    [weakSelf.tableView reloadData];
                }
                
            }
            
            [weakSelf.tableView.mj_footer endRefreshing];
        } failBlock:^(id object) {
            [weakSelf.tableView.mj_footer endRefreshing];
        }];
    }else if ([self.text isEqualToString:@"结业专题"]){
        [LPRequestManager request_getMyClassListUseWeicatAppWithpageindex:self.page * 10 graduatestatus:@"1" year:self.timeString successBlock:^(id object) {
            NSString *result = [[object objectForKey:@"data"] objectForKey:@"result"];
            if ([result integerValue] == 1) {
                NSArray *array = [[object objectForKey:@"data"] objectForKey:@"dataList"];
                if (array.count == 0) {
                    _page --;
                }else{
                    [weakSelf.dataArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array]] ;
                    [weakSelf.tableView reloadData];
                }
                
            }
            
            [weakSelf.tableView.mj_footer endRefreshing];
        } failBlock:^(id object) {
            [weakSelf.tableView.mj_footer endRefreshing];
        }];
    }else{
        
        [LPRequestManager request_getmyclassscoreWithyear:self.timeString pageindex:self.page  successBlock:^(id object) {
            NSString *code = [[object objectForKey:@"data"] objectForKey:@"result"];
            
            if ([code integerValue] == 1) {
                NSArray *array = [[object objectForKey:@"data"] objectForKey:@"dataList"];
               
                if (array.count == 0) {
                    _page --;
                }else{
                    [weakSelf.dataArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPZWYChooseClassClassModel class] json:array]] ;
                    [weakSelf.tableView reloadData];
                }
            }
            [weakSelf.tableView.mj_footer endRefreshing];
        } failBlock:^(id object) {
            [weakSelf.tableView.mj_footer endRefreshing];
        }];
    }
    
    
    
    
}


#pragma mark - 界面
- (void)layoutUI{
    [self.view addSubview:self.header];
    self.header.delegate = self;
    self.header.sd_layout
    .topSpaceToView(self.navigView, 0)
    .leftEqualToView(self.view)
    .rightEqualToView(self.view)
    .heightIs(60 * [UIScreen zj_screenHScale]);
    self.header.score = @"0";
    self.header.text = self.text;
    
    [self.view addSubview:self.tableView];
    self.tableView.sd_layout
    .leftSpaceToView(self.view , 0)
    .rightSpaceToView(self.view , 0)
    .topSpaceToView(self.header , 0)
    .bottomSpaceToView(self.view , 0);
    
    
}
#pragma mark - LPHeaderDetailHeaderVIewDelegate
- (void)LPHeaderDetailHeaderVIewWithChooseTimeString:(NSString *)timeString{
    self.timeString = timeString;
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark - UITableViewDataSource,UITableViewDelegate
/**  返回组个数  */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
/**  返回cell个数  */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}
/**  返回cell高度  */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 125 * [UIScreen zj_screenHScale];
}
/**  cell初始化  */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     LPZWYChooseClassClassModel *model = self.dataArray[indexPath.row];
    if ([self.text isEqualToString:@"结业专题"]) {
        LPZWYCommonSpecilTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:spicalCell forIndexPath:indexPath];
        cell.isShowEnd = YES;
        cell.model = model;
        return cell;
    }else if ([self.text isEqualToString:@"已获学时"]){
       
        
//        if ([model.classOrCourse integerValue] == 1) {
//            LPZWYCommonSpecilTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:spicalCell forIndexPath:indexPath];
//            cell.model = model;
//            cell.isShowEnd = 0;
//            return cell;
//        }else{
            LPZWYCommonClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:classCell forIndexPath:indexPath];
            cell.isMintue = YES;
            cell.model = model;
            return cell;
//        }
       
    }else{
        LPZWYCommonClassTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:classCell forIndexPath:indexPath];
        cell.model = model;
        return cell;
    }
   
    
    
}

/**  cell的点击事件  */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     LPZWYChooseClassClassModel *model = self.dataArray[indexPath.row];
    if ([self.text isEqualToString:@"结业专题"]) {
        YBProjectDetailViewController *vc = [YBProjectDetailViewController new];
        vc.theModel = model;
        [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
    }else if ([self.text isEqualToString:@"已获学时"]){
        
        
//        if ([model.classOrCourse integerValue] == 1) {
//            YBProjectDetailViewController *vc = [YBProjectDetailViewController new];
//            vc.theModel = model;
//            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
//        }else{
            ZJVidoViewController *vc = [ZJVidoViewController new];
            vc.model = model;
            [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
//        }
        
    }else{
        ZJVidoViewController *vc = [ZJVidoViewController new];
        vc.model = model;
        [[ToolClass getMainNavigationController] pushViewController:vc animated:YES];
    }
    
}

#pragma mark - 懒加载
/**  初始化tableView  */
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_NavViewHeight, self.view.bounds.size.width, self.view.bounds.size.height - LP_NavViewHeight ) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor themeGrayColor];
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.tableFooterView = [[TenGrayShadowView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen zj_screenWith], 10 * [UIScreen zj_screenHScale])];
        if ([self.text isEqualToString:@"结业专题"]) {
             [_tableView registerClass:[LPZWYCommonSpecilTableViewCell class] forCellReuseIdentifier:spicalCell];
        }else{
             [_tableView registerClass:[LPZWYCommonClassTableViewCell class] forCellReuseIdentifier:classCell];
        }
       
        [_tableView adaptiveIOS11];
        
    }
    return _tableView;
}


- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

- (LPHeaderDetailHeaderVIew *)header{
    if (_header == nil) {
        _header = [LPHeaderDetailHeaderVIew new];
        
    }
    return _header;
}
@end
